package com.xc.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xc.demo.common.Constants;
import com.xc.demo.common.Result;
import com.xc.demo.entity.Dict;
import com.xc.demo.entity.Menu;
import com.xc.demo.mapper.DictMapper;
import com.xc.demo.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Resource
    private DictMapper dictMapper;

    @GetMapping("/ids")
    public Result findAllIds(){
        return Result.success(menuService.list().stream().map(Menu::getId));
    }



    //    新增数据
    @PostMapping
    public boolean save(@RequestBody Menu menu){
        return menuService.saveOrUpdate(menu);
    }


    //    删除数据
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Integer id){
        return menuService.removeById(id);
    }


//    查询所有
    @GetMapping
    public Result findAll(@RequestParam(defaultValue = "") String uname){


        return Result.success(menuService.findMenus(uname));
    }



    //    分页查询
    @GetMapping("/page")
    public IPage<Menu> findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam(defaultValue = "") String uname
    ){
        log.info("pageNum={},pageSize={},username={},",pageNum,pageSize,uname);

        IPage<Menu> page= new Page<>(pageNum,pageSize);

        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();

        if(!"".equals(uname)){
            queryWrapper.like("name",uname);
        }


        queryWrapper.orderByAsc("id");

        return menuService.page(page,queryWrapper);
    }


    //    请求图标数据
    @GetMapping("/icons")
    public Result getIcons(){

        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", Constants.DICT_TYPE_ICON);

        return  Result.success(dictMapper.selectList(queryWrapper));
    }



}
